/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uber.netbsd.community.dao;

import java.util.List;
import javax.jdo.Query;

/**
 *
 *  Persistence tier for Person object
 *
 * @author alex
 */
public class PersonManager extends GenericManager<Person>{


    protected PersonManager(){
        super(Person.class);
    }

    

    /** Returns the person with the specified ID, or null if no such person exists. */
    public Person getPerson(int id) {
        return get(id);
    }

    public Integer countExist(String username) {
     Query q = persist.newQuery("SELECT FROM " + clazz.getName() + " WHERE name == :username");
        List results = (List) q.execute(username);
        if (results==null)
            return 0;
        else
            return results.size();
    }

    /** Returns a person with the specified username, if one exists. */
    public Person getPerson(String username) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("__getPerson, class="+ clazz.getName());
        }
         Query q = persist.newQuery("SELECT FROM " + clazz.getName() + " WHERE name == :username");
        List results = (List) q.execute(username);
        if (results==null || results.isEmpty())
            return null;
        return (Person) results.get(0);
        
    }

   

    /** Gets a list of all the people in the system. */
    public List<Person> getAllPeople() {
       return getAll();
    }

    
    
    /**
     * Deletes a person from the system...doesn't do anything fancy to clean up where the
     * person is used.
     */
    public void deletePerson(int id) {
        delete(id);
    }

    private static PersonManager instance;

    public static PersonManager getInstance() {

        if (instance==null)
            instance = new PersonManager();

        return instance;
    }
   
}
